package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDtoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;


@Service
public class SetmealDtoServiceImpl implements SetmealDtoService {


    @Autowired(required = false)
    private SetmealDishMapper setmealDishMapper;
    @Autowired(required = false)
    private SetmealMapper setmealMapper;
    @Autowired(required = false)
    private CategoryMapper categoryMapper;
    /**
     * 新增套餐
     *
     * @param setmealDto
     * @return
     */
    @Override
    @CacheEvict(value = "setmealCache",allEntries = true)//确保数据数据和缓存数据的一致性，增删改需要清空缓存的全部数据
    public Boolean addSetmealDto(SetmealDto setmealDto) {
        //0.补全信息
        setmealDto.setCreateTime(LocalDateTime.now());
        setmealDto.setUpdateTime(LocalDateTime.now());
        setmealDto.setStatus(1);
        //添加套餐
        setmealMapper.addSetmeal(setmealDto);

        //获取套餐菜品，补全菜品信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setCreateUser(setmealDto.getCreateUser());
            setmealDish.setUpdateUser(setmealDto.getCreateUser());
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setSort(0);
        }
        //添加套餐菜品
        setmealDishMapper.addSetmealDish(setmealDishes);
        return true;
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<SetmealDto> findAllByName(Integer page, Integer pageSize, String name) {
        //设置当前页和每页大小
        PageHelper.startPage(page,pageSize);
        //查询所有
        List<SetmealDto> setmealDtoList = setmealMapper.findAllByName(name);
        //补全套餐分类名字
        for (SetmealDto setmealDto : setmealDtoList) {
            Category category = categoryMapper.findCategoryById(setmealDto.getCategoryId());
            setmealDto.setCategoryName(category.getName());
        }
        //封装信息
        PageInfo<SetmealDto> setmealDtoPageInfo = new PageInfo<>(setmealDtoList);
        //封装page
        Page<SetmealDto> setmealDtoPage = new Page<>();
        setmealDtoPage.setRecords(setmealDtoPageInfo.getList());
        setmealDtoPage.setTotal(setmealDtoPageInfo.getTotal());
        return setmealDtoPage;
    }

    /**
     * 数据回显：根据套餐id查找套餐和套餐的菜品
     *
     * @param id
     * @return
     */
    @Override
    public SetmealDto selectSetmealDtlAndSetmealDishById(Long id) {
        SetmealDto setmealDto = setmealMapper.selectSetmealDtlAndSetmealDishById(id);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        for (SetmealDish setmealDish : setmealDishes) {
            int parseInt = Integer.parseInt(setmealDish.getCopies().toString());
            BigDecimal copies = new BigDecimal(parseInt);
            setmealDish.setPrice(setmealDish.getPrice().multiply(copies));
        }
        setmealDto.setSetmealDishes(setmealDishes);
        return setmealDto ;
    }
    /**
     * 根据套餐id修改套餐状态
     *
     * @param ids
     * @return
     */
    @Override
    public Boolean updateStatus(List<Long> ids) {
        return setmealMapper.updateStatus(ids)>0;
    }

    /**
     * 根据套餐id修改套餐状态：启用
     *
     * @param ids
     * @return
     */
    @Override
    public Boolean updateStatusInUse(List<Long> ids) {
        return setmealMapper.updateStatusInUse(ids)>0;
    }

    /**
     * 根据ids删除套餐
     *
     * @param ids
     * @return
     */
    @Override
    @CacheEvict(value = "setmealCache",allEntries = true)//确保数据数据和缓存数据的一致性，增删改需要清空缓存的全部数据
    public Boolean deleteByIds(Long[] ids) {
        if (ids.length==0){
            throw new BusinessException("请选中序号再操作");
        }
        //获取套餐中启用的总数
        Long count = setmealMapper.querySetmealWithStatus(ids);
        //判断所选套餐是否处于售卖状态
        if (count>0){
            //正在售卖，不允许删除
            throw new BusinessException("有套餐正在启用中，请下架后再操作");
        }

        return  setmealMapper.deleteByIds(ids)>0;
    }

    /**
     * 修改套餐和套餐菜品
     *
     * @param setmealDto
     * @return
     */
    @Override
    @Transactional//开启事务
    public Boolean updateSetmealDto(SetmealDto setmealDto) {
        //1.调用setmealMapper修改套餐
        //补全信息
        setmealDto.setUpdateUser(setmealDto.getUpdateUser());
        setmealDto.setUpdateTime(LocalDateTime.now());
        //获取套餐分类名称,并补全套餐分类名称
        Category category = categoryMapper.findCategoryById(setmealDto.getCategoryId());
        setmealDto.setCategoryName(category.getName());
        setmealMapper.updateSetmeal(setmealDto);

        //2.根据套餐id,删除对应的菜品
        setmealDishMapper.deleteSetmealDishes(setmealDto.getId());
        //3.重新添加菜品
        //获取页面修改的菜品内容，补全信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setCreateUser(setmealDto.getUpdateUser());
            setmealDish.setUpdateUser(setmealDto.getUpdateUser());
            setmealDish.setSort(0);
        }
        setmealDishMapper.addSetmealDishes(setmealDishes);
        return true;
    }

    /**
     * 根据套餐分类id查找对应的菜品列表
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<SetmealDto> findSetmealDtoByCategoryId(Long categoryId,Integer status) {
        //获取分类下所有套餐列表
        List<SetmealDto> setmealDtoList = setmealMapper.findSetmealDtoByCategoryId(categoryId, status);
//        for (SetmealDto setmealDto : setmealDtoList) {
//            //获取套餐的菜品
//            List<SetmealDish> setmealDishList = setmealDishMapper.findSetmealDishBySetmealId(setmealDto.getId());
//            //把对应的菜品列表添加到套餐中
//            setmealDto.setSetmealDishes(setmealDishList);
//        }
        return setmealDtoList;
    }

    /**
     * 根据套餐id查找对应的菜品列表
     *
     * @param setmealId
     * @return
     */
    @Override
    public SetmealDto findSetmealDishBySetmealId(Long setmealId) {
        List<SetmealDish> setmealDishList = setmealDishMapper.findSetmealDishBySetmealId(setmealId);
        SetmealDto setmealDto = new SetmealDto();
        setmealDto.setSetmealDishes(setmealDishList);
        return setmealDto;
    }
}
